package com.rui.study.algorithm.L_深入理解JVM_类加载的时机_不会引发_父类静态字段间接访问;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import sun.misc.Launcher;

import java.io.IOException;
import java.io.InputStream;

import static com.rui.study.algorithm.L_深入理解JVM_类加载的时机_不会引发_父类静态字段间接访问.Piter._1MB;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-02-28 08:34
 **/
public class NotInitialization {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotInitialization.class);

    private static int test1() {
        return Piter.value;
    }

    private static void test2() {
        Dad[] array = new Dad[4];
    }


    private static void test3() {
        System.out.println("获取常量" + _1MB);
    }

    private static int test4() {
        return _1MB;
    }

    private static boolean test5() {
        String s = "args";
        return s.intern() == s;
    }

    private static void test6() {

//        ClassLoader loader = Launcher.getLauncher().getClassLoader();
        ClassLoader loader = new MyClassLoader();
        try {
            Class klass = loader.loadClass("com.rui.study.algorithm.L深入理解JVM_类加载的时机_不会引发_父类静态字段间接访问.Dad");
            System.out.println("用户加载的Dad类的加载器：" + klass.getClassLoader().toString() + ", parent:" + klass.getClassLoader().getParent().toString());
        } catch (ClassNotFoundException e) {

        }

        System.out.println("NotInitialization 的类加载器是：" + NotInitialization.class.getClassLoader().toString());
    }

    static class MyClassLoader extends ClassLoader {
        public MyClassLoader() {
            super();
        }

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            String path = name.substring(name.lastIndexOf(".") + 1).concat(".class");
            InputStream in = getClass().getResourceAsStream(path);
            if (in == null) {
                return super.loadClass(name);
            }
            byte[] code;
            try {
                code = new byte[in.available()];
                in.read(code);
            } catch (IOException e) {
                throw new ClassNotFoundException();
            }
            return defineClass(name, code, 0, code.length);
        }

        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            LOGGER.debug("自己加载---------");

            return null;
        }
    }

    private static void test7() {
        {
            byte[] placeHolder = new byte[64 * _1MB];
        }
        int a = 3;
        byte[] placeHolder = new byte[64 * _1MB];
//        System.gc();
    }

    private static void test8() {
        String filename = "test.properties";

        byte[] o = new byte[0];

//        InputStream in = o.getClass().getResourceAsStream(filename);
//        Dad dad = new Dad();
//        InputStream in = dad.getClass().getResourceAsStream(filename);
//        InputStream in = DateUtil.class.getResourceAsStream(filename);
//        InputStream in = NotInitialization.class.getResourceAsStream(filename);
        InputStream in = Launcher.getLauncher().getClassLoader().getResourceAsStream(filename);
//        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
        if (in == null) {
            return;
        }
        byte[] b = new byte[0];
        try {
            b = new byte[in.available()];
            in.read(b);
        } catch (IOException e) {
            e.printStackTrace();
        }

        BASE64Encoder encoder = new BASE64Encoder();
        String encodedStr = encoder.encode(b);
        System.out.println(encodedStr);
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            String decodedStr = new String(decoder.decodeBuffer(encodedStr));
            System.out.println(decodedStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        LOGGER.debug("进入main()...");
        test8();
        LOGGER.debug("走出main()...");
    }

}
